package mrusanov.team

import mrusanov.staff._

object ClubStaff {
	val MAX_COACHES = 3
}

class ClubStaff {

	private var _coachList = List[Coach]()
	private var _coach2teams = Map[TeamLevel, Option[Coach]]()

	def coach2teams = _coach2teams withDefaultValue (None)
	var doctor: Option[Doctor] = None
	var marketingManager: Option[MarketingManager] = None
	var masseur: Option[Masseur] = None
	var prManager: Option[PRManager] = None
	var psychologist: Option[Psychologist] = None
	var buildingManager: Option[BuildingManager] = None

	def allStaff = _coachList ++ List(doctor, marketingManager, masseur, prManager, psychologist, buildingManager).flatten.toList

	def coachList = _coachList

	def coachesWithOptions: List[Option[Coach]] = {
		val vacancies = ClubStaff.MAX_COACHES - _coachList.size
		val options = List.fill(vacancies)(None)
		_coachList.map(Some(_)) ++ options
	}

	def addCoach(coach: Coach) {
		require(coachList.size < ClubStaff.MAX_COACHES, "Too many coaches, not possible to add one more")
		_coachList ::= coach
	}

	def removeCoach(coach: Coach) {
		require(coachList.contains(coach), "Coach list doesn't contain coach")
		_coachList = coachList.filterNot(_ == coach)
	}

	def assignCoachToTeam(coach: Coach, team: TeamLevel) {
		require(_coachList.contains(coach), "Coach is absent in coaches list!")

		_coach2teams = _coach2teams.filterNot(_._1 == team)
		_coach2teams = _coach2teams.filterNot(_._2 == Some(coach))
		_coach2teams += (team -> Some(coach))
	}

	def removeEmployee(employee: ClubEmployee) {
		require(allStaff.contains(employee), "Employee doesn't belong to club")

		employee match {
			case c: Coach => removeCoach(c)
			case d: Doctor => doctor = None
			case m: MarketingManager => marketingManager = None
			case ms: Masseur => masseur = None
			case pr: PRManager => prManager = None
			case p: Psychologist => psychologist = None
			case b: BuildingManager => buildingManager = None
			case _ => throw new IllegalArgumentException("Unable to remove employee " + employee.getClass)
		}
	}
}
